package com.wollo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wollo.domain.PageResult;
import com.wollo.domain.TMenu;
import com.wollo.domain.User;
import com.wollo.mapper.MenuMapper;
import com.wollo.mapper.RoleMapper;
import com.wollo.mapper.UserMapper;
import com.wollo.service.MenuService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * //
 * //   █████▒█    ██  ▄████▄   ██ ▄█▀       ██████╗ ██╗   ██╗ ██████╗
 * // ▓██   ▒ ██  ▓██▒▒██▀ ▀█   ██▄█▒        ██╔══██╗██║   ██║██╔════╝
 * // ▒████ ░▓██  ▒██░▒▓█    ▄ ▓███▄░        ██████╔╝██║   ██║██║  ███╗
 * // ░▓█▒  ░▓▓█  ░██░▒▓▓▄ ▄██▒▓██ █▄        ██╔══██╗██║   ██║██║   ██║
 * // ░▒█░   ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄       ██████╔╝╚██████╔╝╚██████╔╝
 * //  ▒ ░   ░▒▓▒ ▒ ▒ ░ ░▒ ▒  ░▒ ▒▒ ▓▒       ╚═════╝  ╚═════╝  ╚═════╝
 * //  ░     ░░▒░ ░ ░   ░  ▒   ░ ░▒ ▒░
 * //  ░ ░    ░░░ ░ ░ ░        ░ ░░ ░
 * //           ░     ░ ░      ░  ░
 * //                 ░
 *
 * @author Java song
 * @date 2024/9/16 下午8:22
 * @description
 **/
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<TMenu> selectAll(Integer pageNum, Integer pageSize) {
        Page<TMenu> menuPage = PageHelper.startPage(pageNum, pageSize);
        List<TMenu> allMenu = menuMapper.getAllMenu();
        PageResult<TMenu> tMenuPageResult = new PageResult<>();
        tMenuPageResult.setCode(0);
        tMenuPageResult.setMsg("");
        tMenuPageResult.setCount(menuPage.getTotal());
        tMenuPageResult.setData(allMenu);
        return tMenuPageResult;
    }

    @Override
    public List<TMenu> selectBy(TMenu menu ,String username) {
        if(menu==null){
            menu=new TMenu();
            menu.setFid(0);
        }
        User user = userMapper.selectUserInfoByUserNameMapper(username);
        System.out.println(user);
        Integer uid = user.getUid();
        List<Integer> integers = roleMapper.selectRoleIdsByUserIdMapper(uid);
        List<TMenu> menuBy=new ArrayList<>();
        //获取当前用户获得所有菜单
        for (Integer roleId : integers) {
            menu.setRid(roleId);
            List<TMenu> tempList = menuMapper.getMenuBy(menu);
            tempList.forEach(m->{
                if(!menuBy.contains(m)){
                    menuBy.add(m);
                }
            });
        }
        //获取所有菜单
        searchChildrenMenuByMenu(menuBy);

        return menuBy;
    }

    private void searchChildrenMenuByMenu(List<TMenu> menuBy) {
        if (menuBy == null || menuBy.isEmpty()) {
            //没有子菜单
            //退出循环
        }else{
            //有子菜单
            //递归查询子菜单
            for (TMenu m : menuBy) {
                TMenu searchMenu = new TMenu();
                searchMenu.setFid(m.getMid());
                searchMenu.setRid(m.getRid());
                List<TMenu> childrenMenu = menuMapper.getMenuBy(searchMenu);
                m.setChildren(childrenMenu);
                searchChildrenMenuByMenu(childrenMenu);
            }
        }
    }

    @Override
    public int insert(TMenu menu) {
        int i = menuMapper.insertMenu(menu);
        return SUCCESS;
    }

    @Override
    public int update(TMenu menu) {
        int i = menuMapper.updateMenu(menu);
        return SUCCESS;
    }

    @Override
    public int delete(int id) {
        int i = menuMapper.deleteMenu(id);
        return SUCCESS;
    }

    @Override
    public int delete(int[] ids) {
        for (int id : ids) {
            int i = menuMapper.deleteMenu(id);
        }
        return SUCCESS;
    }
}
